package com.eece496.Server;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eece496.Database.Administrator;
import com.eece496.Database.Course;
import com.eece496.Database.Enrollment;
import com.eece496.Database.Student;
import com.eece496.Database.TAEnrollment;
import com.eece496.Database.TeachingAssistant;
import com.eece496.Helpers.CourseInfo;

/**
 * Servlet implementation class LoginServlet
 * Handles all the Logins
 */
public class LoginServlet extends MasterServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException 
			{
		
		// Connect to the Database
		Connect();
		
		//Instantiate our DB objects
		Student StudentDB = new Student();
		StudentDB.Setup(connection);
		TeachingAssistant TADB = new TeachingAssistant();
		TADB.Setup(connection);
		Administrator AdminDB = new Administrator();
		AdminDB.Setup(connection);
		
		// Get paramaters from the login form
		String username = request.getParameter("username");
		String password = request.getParameter("password"); //-----WILL BE REMOVED
		String role = request.getParameter("role");
		
		// Check the role first
		//Student Role
		if (role.equals("Student")) 
		{
			try {
				//Check if the user exists in the student database
				if (StudentDB.UserExists(username) == true) {
					
					//Check if the password is right ------ THIS WILL BE REMOVED!! The DB will NOT STORE PASSWORDS!!!!
					if(password.equals(StudentDB.GetPassword()))
					{
						//Variable for determining if the student has an empty course list
						String isEmpty = "false";
						ArrayList<CourseInfo> info = new ArrayList<CourseInfo>();
						
						Enrollment myEnrolledCourses = new Enrollment();
						myEnrolledCourses.Setup(connection);
						
						//Get student information
						int studentID = StudentDB.getStudentID();
						String sID = Integer.toString(studentID);
						info = myEnrolledCourses.GetStudentCourseInfo(studentID);
						String firstname = StudentDB.getFirstName();
						String lastname =  StudentDB.getLastName();
						
						//For debugging
						System.out.println(info.size());
						
						//check if the courselist is empty
						if(info.size() == 0)
						{
							isEmpty.equals("true");
							System.out.println(isEmpty);
						}
						
						//Forward data to jsp
						request.setAttribute("firstname", firstname);		
						request.setAttribute("lastname", lastname);	
						request.setAttribute("CourseInfo", info);		
						request.setAttribute("isEmpty", isEmpty);	
						request.setAttribute("studentID", sID);
						
						request.getRequestDispatcher("/student/student-portal.jsp").forward(
								request, response);	
					}
					else
						request.getRequestDispatcher("index-error.jsp").forward(
								request, response);
				} else {
					request.getRequestDispatcher("index-error.jsp").forward(
							request, response);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} 
		
		//TA role
		else if (role.equals("TA")) 
		{
			//For debugging
			System.out.println(username);
			System.out.println(password);
			
			try {
				//if TA username exists in the Database
				if (TADB.UserExists(username) == true) {
					
					//Check if the password is right ------ THIS WILL BE REMOVED!! The DB will NOT STORE PASSWORDS!!!!
					if(password.equals(TADB.GetPassword()))
					{
						//Variable for determining if the student has an empty course list
						String isEmpty = "false";
						ArrayList<CourseInfo> info = new ArrayList<CourseInfo>();
						
						TAEnrollment myEnrolledCourses = new TAEnrollment();
						myEnrolledCourses.Setup(connection);
						
						//Get TA information
						int TAID = TADB.GetID();
						//System.out.println(TAID);
						String tId = Integer.toString(TAID);
						info = myEnrolledCourses.GetTACourseInfo(TAID);
						String firstname = TADB.getFirstName();
						String lastname =  TADB.getLastName();
						
						//Check if the TA has any enrolled courses
						if(info.size() == 0)
						{
							isEmpty.equals("true");
							System.out.println(isEmpty);
						}
						
						//Forward info to jps
						request.setAttribute("TAID", tId);
						request.setAttribute("firstname", firstname);		
						request.setAttribute("lastname", lastname);	
						request.setAttribute("CourseInfo", info);		
						request.setAttribute("isEmpty", isEmpty);	
					
						request.getRequestDispatcher("/TA/TA-portal.jsp").forward(
								request, response);
					}
					
					else
					{
						request.getRequestDispatcher("index-error.jsp").forward(
								request, response);
					}
				}

				else {
					request.getRequestDispatcher("index-error.jsp").forward(
							request, response);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} 
		
		//Prof role
		else if (role.equals("Professor")) 
		{
			try {
				
				//If if prof exists in the Database
				if (AdminDB.UserExists(username) == true) {
					
					//Check if the password is right ------ THIS WILL BE REMOVED!! The DB will NOT STORE PASSWORDS!!!!
					if(password.equals(AdminDB.GetPassword()))
					{
						//Variable for determining if the student has an empty course list
						String isEmpty = "false";
						
						//Get course List
						ArrayList<CourseInfo> info = new ArrayList<CourseInfo>();
						Course CourseDB = new Course();
						CourseDB.Setup(connection);
						info = CourseDB.GetAllCourseInfo();
						
						//Get Prof Info
						String firstname = AdminDB.getFirstName();
						String lastname =  AdminDB.getLastName();
						
						//check if the prof has an empty courselist
						if(info.size() == 0)
						{
							isEmpty.equals("true");
							System.out.println(isEmpty);
						}
						
						//Forward to JSP
						request.setAttribute("firstname", firstname);		
						request.setAttribute("lastname", lastname);	
						request.setAttribute("CourseInfo", info);		
						request.setAttribute("isEmpty", isEmpty);	
						
						request.getRequestDispatcher(
								"/Professor/Professor-Portal.jsp").forward(
										request, response);
					}
					
					else
					{
						request.getRequestDispatcher("index-error.jsp").forward(
								request, response);
					}
				}

				else {
					request.getRequestDispatcher("index-error.jsp").forward(
							request, response);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		// If role is not selected then user is directed to error page
		else {
			request.getRequestDispatcher("index-error.jsp").forward(request,
					response);
		}
		
	}
}
